local skynet = require "skynet"
local cjson = require "cjson"
require "base_api"

local hand_user_info = {}

local GuestIdBase = 800000000

--============================= temp func start =============================--
--暂时不需要token登录了
function hand_user_info.LoginByToken(token, channel)
    local info = cjson.decode(token)
    if info.channel ~= channel then
        log.error("LoginByToken info.channel=", info.channel)
        log.error("LoginByToken channel=", channel)
        return
    end

    local user_id = CallRedis('SafeHget', 'playerinfo:'..math.modf(info.userid), 'userid')
    if not user_id then 
        return 
    end

    return true, tonumber(user_id)
end

--============================= finish func start =============================--
--生成token
local function gen_token_by_userid(account, userid, channel, login_acc_type)
    local info = {
        userid = userid,
        account = account,
        channel = channel,
        login_acc_type = login_acc_type
    }
    local res = cjson.encode(info)
    return res
end

--记录正式玩家的token，login_acc_type 1 为手机 2 为账号
function hand_user_info.SaveUserLogin(account, userid, channel, login_acc_type)
    --也要记录channel
    local token = gen_token_by_userid(account, userid, channel, login_acc_type)  
    local key = 'playerinfo:'..userid
    CallRedis("SafeHset", key, 'userid', userid, 'token', token, 'channel', channel, 'account', account)
    
    return true, token
end

--获取游客userid
function hand_user_info.GetGusetId(account, channel)
    local token, first_login
    --通过设备id和channel来索引userid
    local user_id = CallRedis('SafeHget', 'acc:'..account..':'..channel, 'userid' )  
    --游客第一次登录时没有userid,redis生成并存起来
    if not user_id then
        --累加guest_index
        local guest_index = CallRedis('SafeIncr', 'guest_index')
        --log.info("get_guset_id guest_index=", guest_index)
        user_id = tonumber(guest_index) + GuestIdBase
        CallRedis('SafeHset', 'acc:'..account..':'..channel, 'userid', user_id )
        --生成token
        token = gen_token_by_userid(account, user_id, channel)
        --log.info("token=", token)
        --存"playerinfo:xxxxx" 哈希key-value
        CallRedis('SafeHset', 'playerinfo:'..user_id, 'userid', user_id, 'token', token, 'channel', channel)
        first_login = true
    else
        --取到游客userid
        token = CallRedis('SafeHget', 'playerinfo:'..user_id, 'token' )
    end

    return true, {user_id=tonumber(user_id), token = token, first_login=first_login}
end

local function get_query_types()
    local id_to_name = {
        [0] = "全部类型",
        [1] = "充值",
        [4] = "推广收益",
        [5] = "活动奖励",
        [9] = "俱乐部收益",
    }

    local the_query_types = {}
    for k, v in pairs(id_to_name) do
        local item = {}
        item.id = k
        item.name = v
        table.insert(the_query_types, item)
    end
    return the_query_types
end

--获取游客的信息
function hand_user_info.GetGuestInfo(userid)            
    local key = string.format('playerinfo:%d', userid)
    --local gold = redis_tool.safe_hget(key, 'gold')
    --local channel = redis_tool.safe_hget(key, 'channel')
    --local nickname = redis_tool.safe_hget(key, 'nickname')
    local gold = CallRedis('SafeHget', key, 'gold')
    local channel = CallRedis('SafeHget', key, 'channel')
    local nickname = CallRedis('SafeHget', key, 'nickname')
    local headimgurl = CallRedis('SafeHget', key, 'headimgurl')
    local sex = CallRedis('SafeHget', key, 'sex')

    if not nickname then
        nickname = gen_nickname()
    end
    if not headimgurl or not sex then
        headimgurl, sex = gen_headimgurl()
    end

    local res = {}
    res.userid = userid
    res.gold = gold
    res.channel = channel
    res.nickname = nickname
    res.headimgurl = tostring(headimgurl)
    res.sex = sex
    res.vip_level = 0
    res.query_types = get_query_types()
    return true, res
end

--注册用户
function hand_user_info.RegisterUser(userid, givenGold)
    if givenGold then
        local key = string.format('playerinfo:%d', userid)
        CallRedis('SafeHset', key, 'gold', givenGold)
    end

    return true
end

--玩家信息入redis缓存
function hand_user_info.SaveUserInfoCache(userid, res)
    local key = 'playerinfo'..':'..userid
    local key_vals = {}
    if res.gold then
        table.insert(key_vals, 'gold')
        table.insert(key_vals, res.gold)
    end
    --上面缓存channel供大厅用,这个供游戏用
    if res.channel then                      
        table.insert(key_vals, 'channel')
        table.insert(key_vals, res.channel)
    end
    if res.nickname then
        table.insert(key_vals, 'nickname')
        table.insert(key_vals, res.nickname)
    end
    if res.headimgurl then
        table.insert(key_vals, 'headimgurl')
        table.insert(key_vals, res.headimgurl)
        if tonumber(res.headimgurl) >= 200 then
            res.sex = g_sex.Female
        else
            res.sex = g_sex.Male
        end
    end
    if res.vip_level then
        table.insert(key_vals, 'vip_level')
        table.insert(key_vals, res.vip_level)
    end
    if res.lately_login_address then
        table.insert(key_vals, 'lately_addr')
        table.insert(key_vals, res.lately_login_address)
    end

    --log.info("key_vals=", key_vals)
    --多个key val一起传
    if next(key_vals) then   
        CallRedis("SafeHset", key, table.unpack(key_vals)) 
    end

    return true
end

return hand_user_info